import java.io.File;

/**
 * @Author: Wy
 * @Email: DysonWy@foxMail.com
 * @Time: 2020/2/26 19:38
 * @Description:
 */
public class RenameFiles {
    public static void main(String[] args) {
        String dirPath = "H:\\动力节点\\架构师课\\课程视频\\第一期\\jvm";
        String msg = rename(dirPath);
        System.out.println(msg);
    }

    private static String rename(String dirPath) {
        File file = new File(dirPath);
        if (!file.exists()) {
            return "文件不存在";
        }
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            return "文件不存在";
        }
        for (File childFile : files) {
            if (childFile.isDirectory()) {
                rename(childFile.getAbsolutePath());
            } else {
                String childFileName = childFile.getName();
                String flag = childFileName.split("_")[0].split("-")[0];
                if (flag.length() > 4) {
                    continue;
                }
                String childFilePath = childFile.getParent();
                String newName = fileNameFactory(childFileName);
                File newFile = new File(childFilePath + "\\" + newName);
                boolean b = childFile.renameTo(newFile);
                System.out.println(b);
            }
        }
        return "";
    }

    private static String fileNameFactory(String fileName) {
        String[] nameSplit = fileName.split("_");
        String[] childName = nameSplit[0].split("-");
        StringBuilder sb = new StringBuilder();
        for (int i = childName.length - 1; i >= 0; i--) {
            sb.append(childName[i]);
            if (i > 0) {
                sb.append("-");
            }
        }
        sb.append("_");
        sb.append(nameSplit[1]);
        return sb.toString();
    }
}
